package com.ajou.adonate.servlet;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

import javax.mail.Session;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ajou.adonate.imap.*;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import java.util.*;

/**
 * Servlet implementation class FileUploadServlet
 */
@WebServlet("/FileUploadServlet")
public class FileUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileUploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("FileUploadServlet : doPost()");
		
		String uploadPath = "/Users/tapaha7/upload/";
		
		int sizeLimit = 100 * 1024 * 1024 ;
		HttpSession session = request.getSession(true);
		
		String mId = "adonate.ajou";
		String mPw = "ajou.adonate";
		
		try{
			MultipartRequest multi = new MultipartRequest(request, uploadPath, sizeLimit, "UTF-8" ,new DefaultFileRenamePolicy());
			Enumeration formNames = multi.getFileNames();
			String formName = (String)formNames.nextElement();
			String fileName = multi.getFilesystemName(formName);
			String originalName = multi.getOriginalFileName(formName);
			
			//fileName = URLDecoder.decode(fileName, "UTF-8");
			//System.out.println("UTF-8 fileName : " + URLDecoder.decode(fileName, "UTF-8"));
			//System.out.println("EUC-KR fileName : " + URLDecoder.decode(fileName, "EUC-KR"));
			String email_account = multi.getParameter("email_account");
			String title = multi.getParameter("title");
			String author = multi.getParameter("author");
			String description = multi.getParameter("description");
		
			if(fileName == null) {
				System.err.println("File Upload Error");
			} else {
				System.out.println("file Name : " + fileName);

				UserSession us = new UserSession();
				if (us.ConnectSession(mId, mPw) == true) {
					System.out.println("us.ConnectSession == true");
				}
	
				uploadPath = uploadPath + fileName;
				
				System.out.println("uploadPath : " + uploadPath);
				System.out.println("title : " + title);
				System.out.println("author : " + author);
				System.out.println("description : " + description);
				
				MailSender Sender = new MailSender(us);
				Sender.SendNewMail(fileName, uploadPath, title, author, description, email_account);
				
				response.sendRedirect("index.jsp");
			} // end if
		} catch(Exception e) {
			System.err.println(e);
		} 
	}
}
